package com.wxdc.nmgyt.nmgytwydc.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.PhotoView;
import com.wxdc.nmgyt.nmgytwydc.R;
import com.wxdc.nmgyt.nmgytwydc.presenter.PhotoUploadPersenter;
import com.wxdc.nmgyt.nmgytwydc.presenter.contract.PhotoUploadContract;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.ResponseBody;

import static com.bumptech.glide.request.target.Target.SIZE_ORIGINAL;

/**
 * Created by Administrator on 2018/6/6.
 */

public class PhotoViewActivity extends BaseActivity{
    private final String TAG = this.getClass().getSimpleName();
    public static String SHOWURL = "show_current_url";
    @BindView(R.id.view_pager)
    HackyViewPager mViewPager;
    private int screenWidth;
    private int width;

    @Override
    protected int getLayout() {
        return R.layout.activity_photoview;
    }

    @Override
    protected void initInject() {

    }
    private List<String> urls;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        screenWidth = mContext.getResources().getDisplayMetrics().widthPixels;//屏幕的宽度(px值）
        width = screenWidth / 3;//Item的宽度，或图片的宽度
        EventBus.getDefault().register(this);//这句话一定不要放在 初始化组件之前
        mViewPager.setAdapter(new SamplePagerAdapter());
        String url_address = getIntent().getStringExtra(SHOWURL);
        for (int i = 0; i < urls.size(); i++) {
            if (url_address.equals(urls.get(i))) {
                mViewPager.setCurrentItem(i);
                break;
            }
        }
    }

    class SamplePagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return urls.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            PhotoView photoView = new PhotoView(container.getContext());
            //photoView.setImageURI(Uri.fromFile(new File(urls.get(position))));
            // Now just add PhotoView to ViewPager and return it
            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            Glide.with(mContext)
                    .load(new File(urls.get(position)))
                    .override(width,SIZE_ORIGINAL)
                    .fitCenter()//缩放图像测量出来等于或小于ImageView的边界范围,该图像将会完全显示
                    .into(photoView);
            photoView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }

    public static void startActivity(Context context, String url) {
        Intent intent = new Intent(context, PhotoViewActivity.class);
        intent.putExtra(SHOWURL, url);
        context.startActivity(intent);
    }
    /*
      *
      * */
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void dataEvent(List<String> myurls) {
        urls = myurls;
    }
    @Override
    public void showError(String msg) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
